package com.falcon.util;

import com.falcon.domain.DependencyNode;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author zongf
 * @date 2023-12-18
 **/
public class PomUtil {

    /** 获取 pom 的 Dom 操作对象 */
    public static Document getDocument(String pomAbsPath) {
        try {
            File pomFile =  new File(pomAbsPath);
            Document document =  new SAXReader().read(pomFile);
           return document;
        } catch (Exception ex) {
            throw new RuntimeException(pomAbsPath + " 文件不存在", ex);
        }
    }

    /** 获取pom 中所有的依赖列表*/
    public static List<DependencyNode> getDependencies(String pomAbsPath) {
        try {

            Document document = getDocument(pomAbsPath);

            // dependencies 节点
            Element dependencies = document.getRootElement().element("dependencies");

            // 获取 dependency 节点
            List<Element> dependencyList = dependencies.elements("dependency");

            return ConvertUtil.convert(dependencyList);
        } catch (Exception ex) {
            throw new RuntimeException(pomAbsPath + " 解析失败", ex);
        }
    }

    /** pom 文档中追加依赖列表 */
    public static void appendDependencies(Document document, List<DependencyNode> dependencyDos) {

        Element dependencies = document.getRootElement().element("dependencies");
        //清空
        dependencies.clearContent();

        for (DependencyNode dependencyDo : dependencyDos) {
            dependencies.addText("\n");

            // 开始
            dependencies.addText("\t\t");
            Element dependency = dependencies.addElement("dependency");
            dependency.addText("\n");

            dependency.addText("\t\t\t");
            Element groupId = dependency.addElement("groupId");
            groupId.setText(dependencyDo.getGroupId());
            dependency.addText("\n");

            dependency.addText("\t\t\t");
            Element artifactId = dependency.addElement("artifactId");
            artifactId.setText(dependencyDo.getArtifactId());
            dependency.addText("\n");

            if (StringUtils.isNotEmpty(dependencyDo.getVersion())) {
                dependency.addText("\t\t\t");
                Element version = dependency.addElement("version");
                version.setText(dependencyDo.getVersion());
                dependency.addText("\n");
            }

            if (StringUtils.isNotEmpty(dependencyDo.getScope())) {
                dependency.addText("\t\t\t");
                Element scope = dependency.addElement("scope");
                scope.setText(dependencyDo.getVersion());
                dependency.addText("\n");
            }

            // 结束
            dependency.addText("\t\t");

            dependencies.addText("\n\t");
        }
    }

    /** 将 pom 文档写入文件, 生成新的 pom.xml 文件*/
    public static void write(Document document, String filePath) {
        try {
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filePath));
            xmlWriter.write(document);
            xmlWriter.flush();
        } catch (Exception ex) {
            throw new RuntimeException("生成新pom.xml 异常", ex);
        }
    }

}
